package softswitch.service;

import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import softswitch.model.CallSummaryDisposition;
import softswitch.model.Circuit;


@Stateless
public class CallSummaryDispositionDAO {

	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
		
	
	public CallSummaryDisposition getById(Integer id){	
		return (CallSummaryDisposition) em.find(CallSummaryDisposition.class, id);
	}
	
	public CallSummaryDisposition save(CallSummaryDisposition callsummarydisposition) {
    	em.persist(callsummarydisposition);
    	return callsummarydisposition;
    }
	
	public CallSummaryDisposition update(CallSummaryDisposition callsummarydisposition) {
    	return em.merge(callsummarydisposition);
    }
    
	public void delete(Integer id) {
    	em.remove(em.find(CallSummaryDisposition.class, id));	
    }
    
    //----------------------------------------------------------------------
	
	public void deleteByDateRange(Date startdate, Date enddate,Circuit circuit ) {	
		String sql = "DELETE FROM CallSummaryDisposition e WHERE e.date BETWEEN :startdate AND :enddate ";
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		Query query = em.createQuery(sql);
		query.setParameter("startdate", startdate);
		query.setParameter("enddate", enddate);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
        query.executeUpdate();   
    }
	
    public CallSummaryDisposition getByDateRange(Date startdate, Date enddate, Circuit circuit, String disposition) {	

    	String sql = "SELECT e FROM CallSummaryDisposition e WHERE e.date BETWEEN :startdate AND :enddate ";
		
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		if(disposition!=null){
			sql = sql + " AND e.disposition=:disposition ";
		}
		
		Query query = em.createQuery(sql);
		query.setParameter("startdate", startdate);
		query.setParameter("enddate", enddate);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
		if(disposition!=null){
			query.setParameter("disposition", disposition);
		}
		
		try{
			return (CallSummaryDisposition) query.getSingleResult();
		}
		catch(Exception e){
			return null;
		}
		
		
    }

    
	public Long getTotalCalls(Date startdate, Date enddate, Circuit circuit, String disposition ) {
		
		String sql = " SELECT SUM(e.calls) FROM CallSummaryDisposition e WHERE  e.date BETWEEN :startdate AND :enddate ";
		
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		if(disposition!=null){
			sql = sql + " AND e.disposition=:disposition ";
		}

		Query query = em.createQuery(sql);
		query.setParameter("startdate", startdate);
		query.setParameter("enddate", enddate);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
		if(disposition!=null){
			query.setParameter("disposition", disposition);
		}
		
		try{
			return (Long)query.getSingleResult();	
		}
		catch(Exception e){
			return null;
		}
    	
    }
    
    
}
